/***********************************************************
 * Product Name: ExcelQuicker
 * Copyright (c)2005, Eunge
 * Author: Eunge
 * Web Site: www.eudev.net
 * Mail/MSN: eudev.net@yeah.net
 * ********************************************************/
using System;

namespace ExcelQuicker.Framework
{
	/// <summary>
	/// Image class. Output image to the excel document
	/// </summary>
	/// <example>
	/// <br>//For example, you have a image named: c:\pictures\sun.jpg. And you want to output it on the (2,2) cell of the excel document.</br>
	/// <br>EQImage img = new EQImage(@"c:\pictures\sun.jpg", 2, 2);</br>
	/// <br>//img.Width = ?; img.Height = ?;  You can resize it by yourself here</br>
	/// <br>objEQWorksheet.Add(img);</br>
	/// <br>objEQApplication.Add(img);</br>
	/// </example>
	[Serializable]
	public class EQImage: EQGraphic 
	{
		private string mFileName = "";

		/// <summary>
		/// Initialize object by specializing file name and position
		/// </summary>
		/// <param name="filename">Full file name</param>
		/// <param name="top">First row index of left-top corner of the image</param>
		/// <param name="left">First column index of left-top corner of the image</param>
		public EQImage(string filename, int top, int left)
		{
			this.mFileName = filename;
			this.Top = top;
			this.Left = left;
		}

		/// <summary>
		/// Output image to the excel document
		/// </summary>
		/// <param name="objWorksheet"></param>
		protected internal override void Output(Excel.Worksheet objWorksheet)
		{
			this.DoBeforeOperationEvent(objWorksheet);

			Excel.Range objRange = (Excel.Range)objWorksheet.Cells[this.Top, this.Left];	//Select the cell
			double left = double.Parse(objRange.Left.ToString(), EQKit.ObjNumberFormatInfo);	//Convert column index to pixel on the excel document
			double top = double.Parse(objRange.Top.ToString(), EQKit.ObjNumberFormatInfo); //Convert row index to pixel on the excel document

			Excel.Pictures objPictures = (Excel.Pictures)objWorksheet.Pictures(this.Missing);
			Excel.Picture objPicture = objPictures.Insert(mFileName,this.Missing);

			base.Output(objWorksheet);

			//Select the image and set it's measure
			objPicture.Select(this.Missing);
			objPictures.ShapeRange.Item(objPicture.Index).Width = this.Width;
			objPictures.ShapeRange.Item(objPicture.Index).Height = this.Height;
			objPictures.ShapeRange.Item(objPicture.Index).Top = (float)top;
			objPictures.ShapeRange.Item(objPicture.Index).Left = (float)left;

			this.DoAfterOperationEvent(objWorksheet);
		}
	}
}
